package com.quasar.hpatient.view.personal.collection;

import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.CarouselData;
import com.quasar.hpatient.model.medicalmodel.CollectionListData;
import com.quasar.hpatient.model.medicalmodel.ConsultationBean;
import com.quasar.hpatient.onInterface.OnConsultationListener;
import com.quasar.hpatient.presenter.RecommendListPresenter;
import com.quasar.hpatient.view.adapter.CollectionAdapter;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 我的收藏
 */
@EActivity(R.layout.activity_collection)
public class CollectionActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener,
        OnConsultationListener {

    public CollectionAdapter collectionAdapter;
    private int delayMillis = 1000;//设置刷新等待的时间
    private RecommendListPresenter recommendListPresenter;
    int page = 1;
    //判断下一页是否存在数据
    private boolean onLoadMore = false;

    @ViewById(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;

    @AfterViews
    void init() {
        initView();
        ViseLog.d("enter");
    }

    @Override
    protected void initView() {
        initToolbar();
        initData();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_list.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        recommendListPresenter = new RecommendListPresenter(this);
        initAdapter();
    }

    //初始化adapter布局
    public void initAdapter() {
        collectionAdapter = new CollectionAdapter();
        collectionAdapter.setOnLoadMoreListener(this, rv_list);
        collectionAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        rv_list.setAdapter(collectionAdapter);
        //初始化数据
        recommendListPresenter.MyRecommendListData(page + "");
        //item点击
        rv_list.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                CounsultWebViewActivity_.intent(CollectionActivity.this).recordId(collectionAdapter.getItem(position).getNoteId() + "").start();
            }
        });
    }

    //下拉刷新
    @Override
    public void onRefresh() {
        page = 1;
        recommendListPresenter.MyRecommendListData(page + "");
        collectionAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //下拉后添加新数据
                swipeLayout.setRefreshing(false);//下拉刷新关闭
                collectionAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }


    //上拉加载更多
    @Override
    public void onLoadMoreRequested() {
        page++;//下拉时page递增
        recommendListPresenter.MyRecommendListData(page + "");
        rv_list.postDelayed(new Runnable() {
            @Override
            public void run() {
                //判断当返回的数据为空时停止下拉刷新
                if (onLoadMore) {
                    collectionAdapter.loadMoreEnd();
                } else {
                    collectionAdapter.loadMoreComplete();
                    collectionAdapter.notifyDataSetChanged();
                }
            }

        }, delayMillis);
    }

    @Override
    public void onConsultationListSuccess(AnotherResult<ConsultationBean> data) {

    }

    @Override
    public void onError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onConsultationRefreshUI() {
        collectionAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCarouselSuccess(AnotherResult<CarouselData> dataAnotherResult) {

    }

    @Override
    public void onMyConsultationListSuccess(AnotherResult<CollectionListData> dataAnotherResult) {
        //判断page是否是下拉还是上拉
        if (page == 1) {
            collectionAdapter.setNewData(dataAnotherResult.getList());
            onLoadMore = false;
        } else {
            collectionAdapter.addData(dataAnotherResult.getList());
            if (dataAnotherResult.getList().size() == 0) {
                onLoadMore = true;
            }
        }
    }

    @Override
    public void onNottoCollectSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void onButtonCollectionSuccess(DataStateResult dataStateResult) {

    }

}
